package com.appbee.songs.client.ui;

import javax.inject.Inject;

import com.appbee.core.client.ui.ISimpleFilterView;
import com.appbee.gdata.youtube.client.cell.AddEntryListener;
import com.appbee.gdata.youtube.client.cell.EntryMenuCell;
import com.appbee.gdata.youtube.client.design.cells.YoutubeCellListResources;
import com.appbee.gdata.youtube.client.presenter.YoutubeDataProvider;
import com.appbee.gdata.youtube.client.search.Entry;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.Range;
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
import com.google.gwt.view.client.SingleSelectionModel;

public class YoutubeSearchPopup extends Composite implements ISimpleFilterView
{

	private static YoutubeSearchPopupUiBinder uiBinder = GWT
			.create(YoutubeSearchPopupUiBinder.class);

	interface YoutubeSearchPopupUiBinder extends
			UiBinder<Widget, YoutubeSearchPopup>
	{
	}
	
	@UiField()
	TextBox searchBox;
	
	@UiField(provided=true)
	CellList<Entry> resultList;
	
	@UiField(provided=true)
	SimplePager pager;
	
	private SingleSelectionModel<Entry> selectionModel;
	private EntryMenuCell entryCell;
	private PopupPanel popupPanel = new PopupPanel();

	@Inject
	public YoutubeSearchPopup(YoutubeDataProvider dataProvider,
			EntryMenuCell entryCell,
			YoutubeCellListResources listRes 
			)
	{
		this.entryCell = entryCell;
		this.resultList = new CellList<Entry>(entryCell,listRes);
		
		selectionModel = new SingleSelectionModel<Entry>();
		resultList.setSelectionModel(selectionModel);
		
		pager = new SimplePager();
		resultList.setPageSize(9);
		pager.setDisplay(resultList);
		
		popupPanel.setAutoHideEnabled(true);
		popupPanel.setAnimationEnabled(true);
		initWidget(uiBinder.createAndBindUi(this));
		popupPanel.setWidget(this);
		popupPanel.setWidth("400px");
		searchBox.addKeyUpHandler(new KeyUpHandler()
		{

			@Override
			public void onKeyUp(KeyUpEvent event)
			{
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER)
				{
					resultList.setVisibleRangeAndClearData(new Range(0, 9),
							true);
				}
			}
		});
		dataProvider.setFilterView(this);
		dataProvider.addDataDisplay(resultList);
	}
	
	public void showRelativeTo(UIObject target)
	{
		popupPanel.showRelativeTo(target);
		Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand()
		{
			
			@Override
			public void execute()
			{
				searchBox.setFocus(true);
			}
		});
	}
	
	public void setEntryListener(AddEntryListener listener)
	{
		entryCell.setListener(listener);
	}
	
	public void addSelectionChangeHandler(Handler handler)
	{
		selectionModel.addSelectionChangeHandler(handler);
	}
	
	public Entry getSelectedTrack()
	{
		return selectionModel.getSelectedObject();
	}

	@Override
	public String getSearchValue()
	{
		return searchBox.getValue();
	}

	public void clearData()
	{
		searchBox.setValue("");
		resultList.setVisibleRangeAndClearData(new Range(0, 1), false);
	}


}
